﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace serie3_malprogramm_offScreen
{
    public partial class Form1 : Form
    {

        Color buttonActiveColor = Color.PaleVioletRed;
        enum Tool { Pen, Rubber, Line, Rectangle, Ellipse };
        Tool tool;
        int xFirst, yFirst;
        Bitmap bitmap;
        Bitmap currentActionBitmap;
        Graphics cABitmapGraphics;
        Graphics panelGraphics;
        Graphics bitmapGraphics;
        Pen pen = new Pen(Color.Black, 1);
        SolidBrush brush = new SolidBrush(Color.Black);
        Pen rubber = new Pen(Color.White, 20);
        bool draw = false;
        bool fill = false;

        int x1;
        int y1;
        int x2;
        int y2;

        Color background = Color.White;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bitmap = new Bitmap(panel1.Width, panel1.Height);
            currentActionBitmap = new Bitmap(panel1.Width, panel1.Height);
            bitmapGraphics = Graphics.FromImage(bitmap);
            cABitmapGraphics = Graphics.FromImage(currentActionBitmap);
            panelGraphics = panel1.CreateGraphics();
            cABitmapGraphics.Clear(background);

            // Pen aktivieren
            resetButtonsColor();
            button4.BackColor = buttonActiveColor;
            tool = Tool.Pen;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            // entweder hier, oder in panel1_paint!!!
            panelGraphics.DrawImage(bitmap, 0, 0);
            panelGraphics.DrawImage(currentActionBitmap, 0, 0);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            // currentBitmap in dauerhafte Bitmap
            bitmapGraphics.DrawImage(currentActionBitmap, 0, 0);
            // Startpunkt speichern
            xFirst = e.X;
            yFirst = e.Y;
            // Zeichnenmodus
            draw = true;
            // Vorbereitungen nach Werkzeug
            switch (tool)
            {
                case Tool.Pen:
                    break;
                case Tool.Rubber:
                    break;
                case Tool.Line:
                    break;
                case Tool.Ellipse:
                    break;
                case Tool.Rectangle:
                    break;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (draw)
            {
                switch (tool)
                {
                    case Tool.Pen:
                        cABitmapGraphics.DrawLine(pen, xFirst, yFirst, e.X, e.Y);
                        xFirst = e.X;
                         yFirst = e.Y;
                        Invalidate();
                        break;
                    case Tool.Rubber:
                        cABitmapGraphics.DrawLine(rubber, xFirst, yFirst, e.X, e.Y);
                         xFirst = e.X;
                        yFirst = e.Y;
                        Invalidate();
                        break;
                    case Tool.Line:
                        // alte Bitmap zeichnen
                        panelGraphics.DrawImage(bitmap, 0, 0);
                        panelGraphics.DrawImage(currentActionBitmap, 0, 0);
                        // und darüber die "frische" line
                        panelGraphics.DrawLine(pen, xFirst, yFirst, e.X, e.Y);
                        break;
                    case Tool.Ellipse:
                        // alte Bitmap zeichnen
                        panelGraphics.DrawImage(bitmap, 0, 0);
                        // und darüber die "frische" line
                        x1 = Math.Max(xFirst, e.X);
                        y1 = Math.Max(yFirst, e.Y);
                        x2 = Math.Min(xFirst, e.X);
                        y2 = Math.Min(yFirst, e.Y);
                        if (fill)
                            panelGraphics.FillEllipse(brush, x2, y2, x1 - x2, y1 - y2);
                        else
                            panelGraphics.DrawEllipse(pen, x2, y2, x1 - x2, y1 - y2);
                        break;
                    case Tool.Rectangle:
                        // alte Bitmap zeichnen
                        panelGraphics.DrawImage(bitmap, 0, 0);
                        // und darüber die "frische" line
                        x1 = Math.Max(xFirst, e.X);
                        y1 = Math.Max(yFirst, e.Y);
                        x2 = Math.Min(xFirst, e.X);
                        y2 = Math.Min(yFirst, e.Y);
                        if (fill)
                            panelGraphics.FillRectangle(brush, x2, y2, x1 - x2, y1 - y2);
                        else
                            panelGraphics.DrawRectangle(pen, x2, y2, x1 - x2, y1 - y2);
                        break;

                }


            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            switch (tool)
            {
                case Tool.Pen:
                    break;
                case Tool.Rubber:
                    break;
                case Tool.Line:
                    // die endgültige linie in die Bitmap schreiben
                    cABitmapGraphics.DrawLine(pen, xFirst, yFirst, e.X, e.Y);
                    break;
                case Tool.Ellipse:
                    // die endgültige linie in die Bitmap schreiben
                    x1 = Math.Max(xFirst, e.X);
                    y1 = Math.Max(yFirst, e.Y);
                    x2 = Math.Min(xFirst, e.X);
                    y2 = Math.Min(yFirst, e.Y);
                    if (fill)
                        cABitmapGraphics.FillEllipse(brush, x2, y2, x1 - x2, y1 - y2);
                    else
                        cABitmapGraphics.DrawEllipse(pen, x2, y2, x1 - x2, y1 - y2);
                    break;
                case Tool.Rectangle:
                    // die endgültige linie in die Bitmap schreiben
                    x1 = Math.Max(xFirst, e.X);
                    y1 = Math.Max(yFirst, e.Y);
                    x2 = Math.Min(xFirst, e.X);
                    y2 = Math.Min(yFirst, e.Y);
                    if (fill)
                        cABitmapGraphics.FillRectangle(brush, x2, y2, x1 - x2, y1 - y2);
                    else
                        cABitmapGraphics.DrawRectangle(pen, x2, y2, x1 - x2, y1 - y2);
                    break;
            }
            // Zeichenaktion beenden
            draw = false;
            // Bitmap ins Fenster schreiben
            panelGraphics.DrawImage(bitmap, 0, 0);
            panelGraphics.DrawImage(currentActionBitmap, 0, 0);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //             panelGraphics.DrawImage(bitmap, 0, 0);
        }


        private void btnExitClick(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClearClick(object sender, EventArgs e)
        {
            cABitmapGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, bitmap.Width, bitmap.Height);
            Invalidate();
        }

        private void btnUndoClick(object sender, EventArgs e)
        {
            cABitmapGraphics.Clear(Color.Transparent);
            Invalidate();
        }

        private void btnRechteckClick(object sender, EventArgs e)
        {
            resetButtonsColor();
            button8.BackColor = buttonActiveColor;
            tool = Tool.Rectangle;
        }

        private void btnEllipseClick(object sender, EventArgs e)
        {
            resetButtonsColor();
            button7.BackColor = buttonActiveColor;
            tool = Tool.Ellipse;
        }

        private void btnLinieClick(object sender, EventArgs e)
        {
            resetButtonsColor();
            button6.BackColor = buttonActiveColor;
            tool = Tool.Line;
        }

        private void btnRadiererClick(object sender, EventArgs e)
        {
            resetButtonsColor();
            button5.BackColor = buttonActiveColor;
            tool = Tool.Rubber;
        }

        private void btnStiftClick(object sender, EventArgs e)
        {
            resetButtonsColor();
            button4.BackColor = buttonActiveColor;
            tool = Tool.Pen;
        }

        private void btnColorSelectClick(object sender, EventArgs e)
        {
            colorDialog1.FullOpen = true;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.pen.Color = colorDialog1.Color;
                this.brush.Color = colorDialog1.Color;
            }
        }

        private void resetButtonsColor()
        {
            Color color = Color.FloralWhite;
            button4.BackColor = color;
            button5.BackColor = color;
            button6.BackColor = color;
            button7.BackColor = color;
            button8.BackColor = color;
        }

        private void radioFillClick(object sender, EventArgs e)
        {
            if (radioButton1.Checked) fill = true;

        }

        private void radioOutlineClick(object sender, EventArgs e)
        {
            if (radioButton2.Checked) fill = false;
        }


    }
}

